<?php

/**
 * This is the model class for table "{{admin}}".
 *
 * The followings are the available columns in table '{{admin}}':
 * @property integer $admin_id
 * @property string $user_name
 * @property string $user_pwd
 * @property string $real_name
 * @property integer $role_id
 * @property string $encrypt 
 * @property string $last_login_ip
 * @property string $last_login_time
 * @property integer $disabled
 * @property string $lang
 */
class Admin extends CActiveRecord
{
	public $time;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Admin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/* reset db */
	public function getDbConnection()
	{ 
	  self::$db=Yii::app()->db;
	  if(self::$db instanceof CDbConnection) return self::$db;
 	 
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{admin}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_name, user_pwd, encrypt,  disabled, lang', 'required'),
			array('role_id, disabled', 'numerical', 'integerOnly'=>true),
			array('user_name, encrypt', 'length', 'max'=>20),
			array('user_pwd, real_name', 'length', 'max'=>32), 
			array('last_login_ip', 'length', 'max'=>30),
			array('lang', 'length', 'max'=>6),
			array('last_login_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('admin_id, user_name, user_pwd, real_name, role_id, encrypt,   last_login_ip, last_login_time, disabled, lang', 'safe', 'on'=>'search'),
			array('real_name,role_id,time','safe','on'=>'searchbj'),
			
		);
	}

	/**
	 * @return array relational rules.
	 */
	 
	public function relations() {
		return array(
			'role'=>array(self::BELONGS_TO,'Role','role_id'),
		);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'admin_id' => Yii::t('attr','admin_id'),
			'user_name' => Yii::t('attr','user_name'),
			'user_pwd' => Yii::t('attr','user_pwd'),
			'real_name' => Yii::t('attr','real_name'),
			'role_id' => Yii::t('attr','role_id'),
			'encrypt' => Yii::t('attr','encrypt'), 
			'last_login_ip' => Yii::t('attr','last_login_ip'),
			'last_login_time' => Yii::t('attr','last_login_time'),
			'disabled' => Yii::t('attr','disabled'),
			'lang' => Yii::t('attr','lang'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;
		$criteria->with = array('role');
		$criteria->compare('user_name',$this->user_name,true,"or");
		$criteria->compare('real_name',$this->user_name,true,"or");
		$criteria->compare('t.role_id',$this->role_id); 
		$criteria->compare('disabled',$this->disabled);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	//返回语言
	public function getLangOptions()
	{
	   $list=array('zh_cn'=>'中文','en_us'=>'English');
	   return $list;
	}

	public function searchbj()
	{
		$criteria=new CDbCriteria;
		$criteria->compare('real_name',$this->real_name,true,"or");
		$criteria->compare('role_id',$this->role_id);
        $criteria->compare('disabled',$this->disabled);
        $criteria->addNotInCondition('admin_id', array(1,2,3,13,15));
        return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	static public function getPingDao($id){
		$arr = Yii::app()->params['admin'];
		if($id){
			return $arr[$id];
		}else{
			return $arr;
		}
	}

	static public function getTime($id){
		$arr = Yii::app()->params['time'];
		if($id){
			return $arr[$id];
		}else{
			return $arr;
		}
	}

	static public function getrealname($name)
	{
		$res = self::model()->findBySql("select real_name from dt_admin where user_name = '$name'");
		return $res['real_name'];
	}
}